//
//  XYCacheHelper.m
//  PeiJue
//
//  Created by lxy on 2017/10/13.
//  Copyright © 2017年 lixiya. All rights reserved.
//

#import "XYCacheHelper.h"
#import <YYCache.h>

#define dafaultDB @"YYCache_dafaultDB"
#define specialDB @"YYCache_specialDB"

@interface XYCacheHelper ()
@property (nonatomic, strong) YYCache *documentDirCache;//document目录存储系统
@property (nonatomic, strong) YYCache *cacheDirCache;//cache目录存储系统
@end

@implementation XYCacheHelper
+ (instancetype)shareInstance{
    static dispatch_once_t onceToken;
    static XYCacheHelper *cacheHelper = nil;
    dispatch_once(&onceToken, ^{
        cacheHelper = [[XYCacheHelper alloc] init];
    });
    return cacheHelper;
}

- (instancetype)init{
    self = [super init];
    if (self) {
    }
    return self;
}

#pragma mark - Public
- (void)cacheToDocumentDirWithObject:(nullable id<NSCoding>)object forKey:(NSString *)key;{
    [self.documentDirCache setObject:object forKey:key withBlock:^{
        
    }];
}

- (void)cacheToCacheDirWithObject:(nullable id<NSCoding>)object forKey:(NSString *)key{
    [self.cacheDirCache setObject:object forKey:key withBlock:^{
        
    }];
}

- (id)getObjectFromDocumentDirForKey:(NSString *)key{
    return [self.documentDirCache objectForKey:key];
}

- (id)getObjectFromCacheDirForKey:(NSString *)key{
    return [self.cacheDirCache objectForKey:key];
}

- (void)removeCacheFromDocumentDirWithBlock:(void(^)(void))block{
    [self.documentDirCache removeAllObjectsWithBlock:block];
}

- (void)removeCacheFromCacheDirWithBlock:(void(^)(void))block{
    [self.cacheDirCache removeAllObjectsWithBlock:block];
}

#pragma mark - Getter
- (YYCache *)documentDirCache{
    if (!_documentDirCache) {
        NSString *path = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:dafaultDB];
        _documentDirCache = [YYCache cacheWithPath:path];
    }
    return _documentDirCache;
}

- (YYCache *)cacheDirCache{
    if (!_cacheDirCache) {
        _cacheDirCache = [YYCache cacheWithName:specialDB];
    }
    return _cacheDirCache;
}

@end
